package com.example.teamtribe.common.userinfo;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.utils.HttpUtil;
import com.example.teamtribe.utils.URLStore;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class UserInfoModel {

    private static UserInfoModel instance;
    private UserInfoModel() {}
    public static UserInfoModel getInstance() {
        if (instance == null) {
            synchronized (UserInfoModel.class) {
                if (instance == null) {
                    instance = new UserInfoModel();
                }
            }
        }
        return instance;
    }

    public void delete(String token, int id, final Back back) {
        Map<String, String> params = new HashMap<>();
        params.put("TeamTribeToken", token);
        params.put("id", id + "");
        HttpUtil.post(URLStore.RELATION_DELETE, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    JSONObject object = new JSONObject(response.body().string());
                    int status = object.getInt("code");
                    if (status != 0) {
                        back.failure(HttpUtil.getCodeMeg(status));
                    } else {
                        back.success(null);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
